//
//  UpdatePhoneViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-2-18.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "UpdatePhoneViewController.h"

#define MESSAGE_TAG  1001
#define SUBMIT_TAG    1002

@implementation UpdatePhoneViewController

- (id)init
{
    self = [super initWithNibClass:[UpdatePhoneViewController class]];
    if (self) {
        _dataSource = [[UpdatePhoneDataSource alloc] initWithVC:self];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"修改手机验证"];
    
    [self.view addTarget:self action:@selector(viewTapped:)];
    [_contentView drawBorder];
}

-(void)viewTapped:(id)sender
{
    [self.phoneTF resignFirstResponder];
    [self.verifyTF resignFirstResponder];
}

- (IBAction)buttonAction:(id)sender
{
    UIButton* btn = (UIButton*)sender;
    _tel = _phoneTF.text;
    _code = _verifyTF.text;
    
    if (btn.tag == MESSAGE_TAG) {
        NSString *url = [NSString stringWithFormat:API_UD_MESSAGE,[AppSetting userName],_tel];
        [_dataSource sendSMSWithURL:url];
    }
    
    if (btn.tag == SUBMIT_TAG) {
        if(![self checkFrom]) return;
        
        NSString *url = [NSString stringWithFormat:API_UD_VALIDATE, _tel, _code];
        [_dataSource validateCodeWithURL:url];
    }
}

- (BOOL)checkFrom
{
    if (_phoneTF.text.length <= 0) {
        [AppDelegate showMessage:@"手机号码不能为空！"];
        return NO;
    }
    
    if (_verifyTF.text.length <= 0) {
        [AppDelegate showMessage:@"验证码不能为空!"];
        return NO;
    }
    
    return YES;
}

@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

#define SEND_SMS_TAG 1001
#define VALIDATE_CODE_TAG 1002
#define UPDATE_TEL_TAG 1003

@implementation UpdatePhoneDataSource

- (id)initWithVC:(id)Controller
{
    self = [super init];
    if(self) {
        self.Controller = Controller;
    }
    
    return self;
}

- (void)sendSMSWithURL:(NSString *)url
{
    [SVProgressHUD show];
    [self get:url tag:SEND_SMS_TAG];
}

- (void)validateCodeWithURL:(NSString *)url
{
    [SVProgressHUD show];
    [self get:url tag:VALIDATE_CODE_TAG];
}

- (void)updateTel
{
    NSString *url = [NSString stringWithFormat:API_UPDATE_TEL,[AppSetting userName], _Controller.tel];
    [self get:url tag:UPDATE_TEL_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    NSDictionary *data = [json objectForKey:@"data"];
    
    [self processRequest:data withTag:request.tag];
}

- (void)processRequest:(NSDictionary *)data withTag:(int)tag
{
    int state = [[data objectForKey:@"state"] intValue];
    
    if (tag == SEND_SMS_TAG) {
        if (state == 1) {
            [SVProgressHUD dismissWithSuccess:@"验证码已发送请注意查收"];
        }
        if (state == 2) {
            [SVProgressHUD dismiss];
            [AppDelegate showMessage:@"当前手机号已被注册"];
        }
    }
    
    if (tag == VALIDATE_CODE_TAG) {
        if (state == 1) {
            [self updateTel];
        }
        if (state == 2) {
            [SVProgressHUD dismiss];
            [AppDelegate showMessage:@"手机验证码不正确"];
        }
    }
    
    if (tag == UPDATE_TEL_TAG) {
        if (state == 1) {
            [SVProgressHUD dismissWithSuccess:@"修改成功"];
            [AppSetting setTel:_Controller.tel];
            [_Controller performSelector:@selector(leftButtonClick:) withObject:nil afterDelay:1.5];
        }
        if (state == 2) {
            [SVProgressHUD dismissWithError:@"手机修改不成功"];
        }
    }
}

@end
